<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/**
 *  File : listModules.php
 *  called only via ajax 
 *  Add module in the table 'module2Lesson'
 */  
 
checkSession();
if ($session->userType() > 3) // admin, head master & teacher
{
  die('Wrong user rights');
  exit;
}
require '../../includes/classes/WS3tableview.php';

$lesson_id=$_POST['id'];

// check if there is such a lesson
$dbLesson=new WS3_Database();
$q="SELECT `id`,`name` FROM `lesson` WHERE `id`='$id' LIMIT 1";

if ($dbLesson->query($q) &&  $dbLesson->nextRecord() )
{

}

$table=new WS3_TableView('main',400);
$table->setTitle( 'Add Module to Lesson '.$dbLesson->f('name') );
$table->addColumnTH('Theme');
$table->addColumnTH('Name');
$table->addColumn('Add','addModuleLessonUpdate&amp;lesson_id='.$lesson_id);
$table->setAlternativeRows(true);
$table->setInfoRow(true);

// build an array of theme names with their ids
$themeArray=array();
$themeArray[0]='N/A';

$dbTheme=new WS3_Database();
$q="SELECT * FROM `theme` WHERE 1";
if (W3DEBUG) array_push($GLOBALS['W3DEBUG'],$q);

if ($dbTheme->query($q))
{
  while ( $dbTheme->nextRecord() )
  {
    $themeArray[$dbTheme->f('id')]= javaSafeString( $dbTheme->f('name') );
  } 
}
//


// pull the modules from the database
$db=new WS3_Database();
$q="SELECT * FROM `module` WHERE 1 AND `active`='1'";
$count = $db->totalRows("module"); 

if (W3DEBUG) array_push($GLOBALS['W3DEBUG'],$q);
if ($db->query($q))
{
  while ( $db->nextRecord() )
  {
     $table->addRow($db->f('id'), array(
     'Theme' => $themeArray[$db->f('theme_id')], 
     'Name' => javaSafeString( $db->f('name') )
      ));
  } 
  $message="Listing";
}else{
  $message="Error : Empty list";
}
 
$table->addToInfoRow("<p>Count = $count </p>");
echo $table->display();

/** Get menu */
require '../../includes/menus/topModuleLesson.php';
$menu=javaSafeString($menu);
echo "document.getElementById('topmenu').innerHTML='$menu';";

echo "document.getElementById('messages').innerHTML='$message';";
if (W3DEBUG)  $debug=new WS3_Debug();

?>